package com.meida.module.system.provider.service.impl;

import com.meida.common.mybatis.base.service.impl.BaseServiceImpl;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.model.ListModel;
import com.meida.common.mybatis.model.PageModel;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.model.SaveModel;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.common.security.OpenHelper;
import com.meida.common.utils.FlymeUtils;
import com.meida.module.system.client.entity.SysDict;
import com.meida.module.system.client.entity.SysGroup;
import com.meida.module.system.provider.mapper.SysGroupMapper;
import com.meida.module.system.provider.service.SysGroupService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 分组 实现类
 *
 * @author flyme
 * @date 2019-06-27
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysGroupServiceImpl extends BaseServiceImpl<SysGroupMapper, SysGroup> implements SysGroupService {

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody listEntityMap(ListModel model) {
        CriteriaQuery<EntityMap> cq = new CriteriaQuery(model, SysGroup.class);
        Long companyId = OpenHelper.getCompanyId();
        Long userId = OpenHelper.getUserId();
        cq.select(SysGroup.class, "groupId", "groupName");
        cq.eq(SysGroup.class, "targetEntity");
        cq.eq(SysGroup.class, "groupName");
        if (FlymeUtils.isNotEmpty(companyId)) {
            cq.eq(SysGroup.class, "companyId", companyId);
        }
        if (FlymeUtils.isNotEmpty(userId)) {
            cq.eq(SysGroup.class, "userId", userId);
        }
        return baseList(cq);
    }


    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody listByComapny(ListModel listModel) {
        Long companyId = OpenHelper.getCompanyId();
        Long userId = OpenHelper.getUserId();
        CriteriaQuery<EntityMap> cq = new CriteriaQuery(listModel, SysGroup.class);
        cq.select(SysGroup.class, "groupId", "groupName");
        cq.eq(SysGroup.class, "targetEntity");
        if (FlymeUtils.isNotEmpty(userId)) {
            cq.eq(SysGroup.class, "userId", userId);
        }
        if (FlymeUtils.isNotEmpty(companyId)) {
            cq.eq(SysGroup.class, "companyId", companyId);
        }
        return baseList(cq);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public List<EntityMap> selectByGroupId(List<Long> groupIds) {
        CriteriaQuery cq = new CriteriaQuery(entityClass);
        cq.select(SysGroup.class, "groupId", "groupName");
        cq.in(true, "groupId", groupIds);
        return selectEntityMap(cq);
    }

    @Override
    public SysGroup findByGroupCode(String groupCode) {
        CriteriaQuery<SysGroup> cq = new CriteriaQuery(SysGroup.class);
        cq.eq(true, "groupCode", groupCode);
        return getOne(cq);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody pageList(PageModel model) {
        CriteriaQuery cq = new CriteriaQuery(model, SysGroup.class);
        Long companyId = OpenHelper.getCompanyId();
        Long userId = OpenHelper.getUserId();
        cq.select(SysGroup.class, "*");
        cq.eq(SysGroup.class, "groupName");
        cq.eq(SysGroup.class, "targetEntity");
        if (FlymeUtils.isNotEmpty(companyId)) {
            cq.eq(SysGroup.class, "companyId", companyId);
        }
        if (FlymeUtils.isNotEmpty(userId)) {
            cq.eq(SysGroup.class, "userId", userId);
        }
        return basePageList(cq);
    }


    @Override
    public ResultBody add(SaveModel model) {
        CriteriaSave cs = new CriteriaSave(model, SysGroup.class);
        Long companyId = OpenHelper.getCompanyId();
        Long userId = OpenHelper.getUserId();
        cs.put("companyId", companyId);
        cs.put("userId", userId);
        return baseAdd(cs);
    }


}
